﻿Type.registerNamespace('SpryEffectsWrapper');
SpryEffectsWrapper.Effects=function() {
    SpryEffectsWrapper.Effects.initializeBase(this);
    this._effectsList=null;
    this._targetControlId=null;
    this._specialEffectActivationOn=null;
    this._effectActivationDelegate=null;
    this.effectsCluster=null;
    this._runMode=1;
    this._effectsDictionary=[];
}

SpryEffectsWrapper.Effects.prototype={
    // OK to declare value types in the prototype
    get_effectsList: function() {
        return this._effectsList;
    },
    set_effectsList: function(value) {
        this._effectsList=value;
    },
    get_targetControlId: function() {
        return this._targetControlId;
    },
    set_targetControlId: function(value) {
        this._targetControlId=value;
    },
    get_specialEffectActivationOn: function() {
        return this._specialEffectActivationOn;
    },
    set_specialEffectActivationOn: function(value) {
        this._specialEffectActivationOn=value;
    },
    get_runMode: function() {
        return this._runMode;
    },
    set_runMode: function(value) {
        this._runMode=value;
    },
    // events
    add_onPreEffect: function(handler) {
        this.get_events().addHandler('onpreeffect',handler);
    },
    remove_onPreEffect: function(handler) {
        this.get_events().removeHandler('onpreeffect',handler);
    },
    add_onStep: function(handler) {
        this.get_events().addHandler('onstep',handler);
    },
    remove_onStep: function(handler) {
        this.get_events().removeHandler('onstep',handler);
    },
    add_onPostEffect: function(handler) {
        this.get_events().addHandler('onposteffect',handler);
    },
    remove_onPostEffect: function(handler) {
        this.get_events().removeHandler('onposteffect',handler);
    },
    add_onCancel: function(handler) {
        this.get_events().addHandler('oncancel',handler);
    },
    remove_onCancel: function(handler) {
        this.get_events().removeHandler('oncancel',handler);
    },
    add_onToggle: function(handler) {
        this.get_events().addHandler('ontoggle',handler);
    },
    remove_onToggle: function(handler) {
        this.get_events().removeHandler('ontoggle',handler);
    },
    add_onObserver: function(handler) {
        this.get_events().addHandler('onobserver',handler);
    },
    remove_onObserver: function(handler) {
        this.get_events().removeHandler('onobserver',handler);
    },
    dispose: function() {
        for(var num1=0;num1<this._effectsDictionary.length;num1++) {
            var item=this._effectsDictionary[num1];
            item.effect.removeObserver(item.observer);
        }

        delete this._effectsDictionary;
        this._effectsDictionary=null;
        // be sure to call base.dispose()
        SpryEffectsWrapper.Effects.callBaseMethod(this,'dispose');
    },
    initialize: function() {
        SpryEffectsWrapper.Effects.callBaseMethod(this,'initialize');
        var elem=null;
        if(this._targetControlId!=null)
            elem=$get(this._targetControlId);
        var eventName=this._specialEffectActivationOn;
        var activateOnPageLoad=(eventName!=null&&eventName=='pageload');
        if((elem!=null&&eventName!=null)&&!activateOnPageLoad) {
            if(this._effectActivationDelegate==null)
                this._effectActivationDelegate==Function.createDelegate(this,this._effectActivationHandler);
            Sys.UI.DomEvent.addHandler(elem,eventName,this._effectActivationDelegate);
        }
        this._createCluster();
        if(activateOnPageLoad)
            this.start();
    },
    _effectActivationHandler: function(sender,args) {
        this.start();
    },
    createCluster: function() {
        this._createCluster();
    },
    _createCluster: function() {
        var elemId=this._targetControlId;
        if(elemId==null) return;

        var elem=$get(elemId);
        if(elem==null) return;

        if(this.effectsCluster==null)
            this.effectsCluster=new Spry.Effect.Cluster({ toggle: true });

        for(var index in this._effectsList) {
            var options=this._effectsList[index];
            var effect=null;
            var skip=true;
            switch(options.effect) {
                case 'Blind':
                    effect=new Spry.Effect.Blind(elemId,options);
                    break;
                case 'Fade':
                    effect=new Spry.Effect.Fade(elemId,options);
                    break;
                case 'Grow':
                    effect=new Spry.Effect.Grow(elemId,options);
                    break;
                case 'Highlight':
                    effect=new Spry.Effect.Highlight(elemId,options);
                    break;
                case 'Shake':
                    effect=new Spry.Effect.Shake(elemId,options);
                    break;
                case 'Slide':
                    //Requires that your element
                    //defined by elemId, has atleast 1 child ELEMENT_NODE (nodeType==1)
                    effect=new Spry.Effect.Slide(elemId,options);
                    break;
                case 'Squish':
                    effect=new Spry.Effect.Squish(elemId,options);
                    break;
                case 'Color':
                    effect=new Spry.Effect.Color(elemId,options.startColor,options.endColor,options);
                    break;
                case 'Move':
                    var fromPos=new Spry.Effect.Utils.Position();
                    fromPos.x=options.fromX;
                    fromPos.y=options.fromY;

                    var toPos=new Spry.Effect.Utils.Position();
                    toPos.x=options.toX;
                    toPos.y=options.toY;

                    Spry.Effect.makePositioned(elem);
                    effect=new Spry.Effect.Move(elemId,fromPos,toPos,options);
                    break;
                case 'Size':
                    var fromRect=new Spry.Effect.Utils.Rectangle;
                    fromRect.width=options.fromWidth;
                    fromRect.height=options.fromHeight;

                    var toRect=new Spry.Effect.Utils.Rectangle;
                    toRect.width=options.toWidth;
                    toRect.height=options.toHeight;

                    Spry.Effect.makeClipping(elem);
                    effect=new Spry.Effect.Size(elemId,fromRect,toRect,options);
                    break;
                case "Fold":
                    effect=new Spry.Effect.Fold(elemId,options);
                    break;
                case "DropOut":
                    effect=new Spry.Effect.DropOut(elemId,options);
                    break;
                case "Puff":
                    effect=new Spry.Effect.Puff(elemId,options);
                    break;
                case "Pulsate":
                    effect=new Spry.Effect.Pulsate(elemId,options);
                    break;
            }
            if(effect!=null) {
                this._registerObservers(effect);
                var runMode=(options.runMode==0)?this._runMode:options.runMode;
                if(runMode<2)
                    this.effectsCluster.addParallelEffect(effect);
                else
                    this.effectsCluster.addNextEffect(effect);
            }
        }
    },
    _registerObservers: function(effect) {
        var o=new Object();
        var context=this;
        var observe=Function.createCallback(context._onObserverHandler,context);
        o.onPreEffect=function(eff) { context._onPreEffectHandler(eff); };
        o.onStep=function(eff) { context._onStepHandler(eff); };
        o.onPostEffect=function(eff) { context._onPostEffectHandler(eff); };
        o.onCancel=function(eff) { context._onCancelHandler(eff); };
        o.onToggle=function(eff) { context._onToggleHandler(eff); };

        effect.addObserver(o);
        effect.addObserver(observe);

        Array.add(this._effectsDictionary,{ effect: effect,observer: o });
        Array.add(this._effectsDictionary,{ effect: effect,observer: observe });
    },
    _onPreEffectHandler: function(eff) {
        var h=this.get_events().getHandler('onpreeffect');
        if(h) h(eff);
    },
    _onStepHandler: function(eff) {
        var h=this.get_events().getHandler('onstep');
        if(h) h(eff);
    },
    _onPostEffectHandler: function(eff) {
        var h=this.get_events().getHandler('onposteffect');
        if(h) h(eff);
    },
    _onCancelHandler: function(eff) {
        var h=this.get_events().getHandler('oncancel');
        if(h) h(eff);
    },
    _onToggleHandler: function(eff) {
        var h=this.get_events().getHandler('ontoggle');
        if(h) h(eff);
    },
    _onObserverHandler: function(notificationType,eff,data,context) {
        var h=context.get_events().getHandler('onobserver');
        if(h) h(notificationType,eff,data);
    },
    start: function() {
        this.effectsCluster.start();
    },
    toggle: function() {
        this.effectsCluster.doToggle();
    },
    cancel: function() {
        this.effectsCluster.cancel();
    }
}

SpryEffectsWrapper.Effects.registerClass('SpryEffectsWrapper.Effects',Sys.Component);

SpryEffectsWrapper.Effects.RunMode=function() { };
SpryEffectsWrapper.Effects.RunMode.prototype=
{
    UseDefault: 0,
    AddParallelEffect: 1,
    AddNextEffect: 2
}
SpryEffectsWrapper.Effects.RunMode.registerEnum('SpryEffectsWrapper.Effects.RunMode');
// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if(typeof (Sys)!=='undefined') Sys.Application.notifyScriptLoaded();
